package com.holyn.guofang.view.my;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.holyn.guofang.R;
import com.holyn.guofang.model.sharepreference.UserInfoSharePreference;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.wo.StatusWo;
import com.holyn.guofang.model.webhttp.wo.UserInfoWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.my.MyContract;
import com.holyn.guofang.presenter.my.MyPresenter;
import com.holyn.guofang.view.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/8/18.
 */

public class MyRegisterInfoUpdateCheckMethodActivity extends BaseActivity implements BaseView, MyContract.UpdateCheckMethodView{
    private static final int REQUEST_ACTION_POST_UPDATE_USER_INFO = 0x0001;
    private static final int REQUEST_ACTION_POST_UN_BIND_CODE = 0x0002;
    private static final int REQUEST_ACTION_POST_BIND_CODE = 0x0003;

    private static final String INTENT_KEY_NEW_CHECK_METHOD = "INTENT_KEY_NEW_CHECK_METHOD";
    private static final String INTENT_KEY_EQUIPMENTCODE = "INTENT_KEY_EQUIPMENTCODE";

    @BindView(R.id.et_old_code)
    EditText etOldCode;
    @BindView(R.id.tv_get_old_code)
    TextView tvGetOldCode;
    @BindView(R.id.et_new_code)
    EditText etNewCode;
    @BindView(R.id.tv_get_new_code)
    TextView tvGetNewCode;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    //REQUEST_ACTION_POST_UPDATE_USER_INFO
    String name;
    String mobile = "";
    String email = "";
    String address;
    String country;
    String area;
    String contact;
    String contactTel;
    String contactMobile;
    String contactEmail;
    String checkMethod = "";
    String newCheckMethod = "";
    String unBindCode = "";
    String bindCode = "";

    //REQUEST_ACTION_POST_BIND_CODE
//    String checkMethod;
    String equipmentCode;

    private UserInfoWo userInfoWo;
    MyContract.Presenter presenter;

    public static void start(Activity activity, String newCheckMethod, String equipmentCode){
        Intent intent = new Intent(activity, MyRegisterInfoUpdateCheckMethodActivity.class);
        intent.putExtra(INTENT_KEY_NEW_CHECK_METHOD, newCheckMethod);
        intent.putExtra(INTENT_KEY_EQUIPMENTCODE, equipmentCode);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_my_register_update_check_method;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        showToolbar("注册信息");

        presenter = new MyPresenter(this);
        presenter.addUpdateCheckMethodView(this);

        initData();
    }

    private void initData(){
        newCheckMethod = getIntent().getStringExtra(INTENT_KEY_NEW_CHECK_METHOD);
        equipmentCode = getIntent().getStringExtra(INTENT_KEY_EQUIPMENTCODE);

        userInfoWo = UserInfoSharePreference.getUserInfo(this);
        UserInfoWo.UserInfoBean userInfo = userInfoWo.getUserInfo();
        name = userInfo.getName();
        if (newCheckMethod.equals("0")){//手机
            mobile = equipmentCode;
        } else if (newCheckMethod.equals("1")){//邮箱
            email = equipmentCode;
        }
        address = userInfo.getAddress();
        country = userInfo.getCountry();
        area = userInfo.getArea();
        contact = userInfo.getContact();
        contactTel = userInfo.getContactTel();
        contactMobile = userInfo.getContactMobile();
        contactEmail = userInfo.getContactEmail();
        checkMethod = userInfo.getCheckMethod();
//        newCheckMethod = "";
//        unBindCode = "";
//        bindCode = "";

    }

    @OnClick({R.id.tv_get_old_code, R.id.tv_get_new_code, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_old_code:
                presenter.postUnBindCode(REQUEST_ACTION_POST_UN_BIND_CODE);
                break;
            case R.id.tv_get_new_code:
                presenter.postBindCode(REQUEST_ACTION_POST_BIND_CODE, newCheckMethod, equipmentCode);
                break;
            case R.id.btn_submit:
                unBindCode = etOldCode.getText().toString().trim();
                if (TextUtils.isEmpty(unBindCode)){
                    toastMsg("解绑验证码不能为空");
                    return;
                }
                bindCode = etNewCode.getText().toString().trim();
                if(TextUtils.isEmpty(bindCode)){
                    toastMsg("绑定验证码不能为空");
                    return;
                }
                presenter.postUpdateUserInfo(REQUEST_ACTION_POST_UPDATE_USER_INFO, name, mobile, email, address, country, area, contact, contactTel, contactMobile, contactEmail, checkMethod, newCheckMethod, unBindCode, bindCode);
                break;
        }
    }

    @Override
    public void requestStart(Integer requestAction) {
        showLoadingDialog();
    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {
        toastMsg("出现异常");
        closeLoadingDialog();
    }

    @Override
    public void requestComplete(Integer requestAction) {
        closeLoadingDialog();
    }

    @Override
    public void responseSuccess(Integer requestAction, StatusWo statusWo) {
        if (requestAction == REQUEST_ACTION_POST_UN_BIND_CODE){
            if (statusWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
                toastMsg("已成功发送解绑验证码");
            } else {
                toastMsg("发送解绑验证码失败");
            }
        } else if (requestAction == REQUEST_ACTION_POST_BIND_CODE){
            if (statusWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
                toastMsg("已成功发送绑定验证码");
            } else {
                toastMsg("发送绑定验证码失败");
            }
        } else if (requestAction == REQUEST_ACTION_POST_UPDATE_USER_INFO){
            if (statusWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
                toastMsg("修改验证方式成功");
                UserInfoSharePreference.savecheckMethod(this, newCheckMethod);
                if (newCheckMethod.equals("0")){//手机
                    UserInfoSharePreference.saveMobile(this, mobile);
                } else if (newCheckMethod.equals("1")){//邮箱
                    UserInfoSharePreference.saveEmail(this, email);
                }
                this.finish();
            } else {
                toastMsg("修改验证方式失败");
            }
        }
    }
}
